package com.fzy.core.tags;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.fzy.core.utils.NumberUtils;

public class ExtGridBodyTag extends BodyTagSupport {

	private static final long serialVersionUID = -949653217908713132L;

	public int doStartTag() throws JspException {
		ExtGridTag grid = (ExtGridTag)getParent();
		List columns = grid.getColumns();
		
		String columnHtml = "";
		int countWidth = 40;
		for (Iterator iterator = columns.iterator(); iterator.hasNext();) {
			String width = (String) iterator.next();
			int intWidth = NumberUtils.toInt(width, 50);
			columnHtml += "						<th style=\"width: "+intWidth+"px; height: 0px;\"></th>";
			countWidth += intWidth;
		}
		
		String html = "";
		html += "<div class=\"x-panel-body x-grid-body x-panel-body-default x-panel-body-default x-layout-fit\" style=\"\">";
		html += "		<div class=\"x-grid-view x-fit-item x-grid-view-default\"";
		html += "			style=\"overflow: visible; margin: 0pt; width: 100%;\"";
		html += "			tabindex=\"-1\">";
		html += "			<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"";
		html += "				style=\"width: "+countWidth+"px; overflow: visible;\"";
		html += "				class=\"x-grid-table x-grid-table-resizer\">";
		html += "				<tbody>";
		html += "					<tr class=\"x-grid-header-row\">";
//		html += "						<th style=\"width: 40px; height: 0px;\"></th>";
		html += columnHtml;
		html += "					</tr>";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}

	public void doInitBody() throws JspException {
		super.doInitBody();
	}

	public int doAfterBody() throws JspException {
		BodyContent bodycontent = getBodyContent();
		String html = bodycontent.getString();
		try {
			bodycontent.getEnclosingWriter().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doAfterBody();
	}

	public int doEndTag() throws JspException {
		String html = "";
		html += "					</tr>";
		html += "				</tbody>";
		html += "			</table>";
		html += "		</div>";
		html += "	</div>";
		html += "	</div>";
		try {
			pageContext.getOut().print(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

}
